/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassandrawindow;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author MT844
 */
public class CheckGeoByBannerid {
        
    public static Map handleData(){
        Map<String,String> retMap = new HashMap<String,String>();        
        
        Map<String,String> geoByBanner = new HashMap<String,String>();        
        MysqlDataManager67 mysql67 = new MysqlDataManager67(); 
        //key:banner and value = location_city
        geoByBanner = mysql67.getAllGeoByBanner();        
        //map city with locaiton
        Map<String,String> cityGeo  = new HashMap<String,String>();
        cityGeo = mysql67.mapCityWithGeo();
        
        for (Map.Entry<String, String> entry : geoByBanner.entrySet()) {
            //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            
            String key = entry.getKey();
            String value = entry.getValue();
            if(!value.equals("_")){                
                String[] arr = new String[2];
                arr = value.split("_");
                
            
                if(!arr[0].equals("")){
                    //neu co vung mien thi add gia tri banner va vung mien vao
                    retMap.put(key, arr[0]);

                } else if (!arr[1].equals("")){
                    StringBuilder locationAd = new StringBuilder() ;
                    //neu ko co vung mien thi convert city vao vung mien                
                    String[] cityids = new String[2];
                    cityids = arr[1].split(","); 
                    for (int i = 0; i < cityids.length ; i++ ){                    
                        String tempSt =   cityGeo.get(cityids[i]);                     
                        if(!locationAd.toString().contains(tempSt)){
                            locationAd.append(tempSt).append(",");                    
                        }
                    }

                //add vung mien unng voi key 
                retMap.put(key, locationAd.toString());

                } else {
                    continue;
                }  
            }
        }
        
        return retMap;
    }
        
}
